//
//  AddTicketMemberCell.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/12/15.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "AddTicketMemberCell.h"
#import "addTicketPersonsCell.h"

@implementation AddTicketMemberCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.tableView.scrollEnabled = NO;
    self.numLabel.text = [NSString stringWithFormat:@"%ld",self.currentNum];
    
    self.tiecktNumView.layer.cornerRadius = 17;
    self.tiecktNumView.clipsToBounds = YES;
    self.tiecktNumView.layer.borderColor = kContentColor.CGColor;
    self.tiecktNumView.layer.borderWidth = 0.f;
    self.numLabel.layer.cornerRadius = 17;
    self.numLabel.clipsToBounds = YES;
    self.numLabel.layer.borderColor = kContentColor.CGColor;;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"addTicketPersonsCell" bundle:nil] forCellReuseIdentifier:@"addTicketPersonsCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ReduceTicketPersonCell" bundle:nil] forCellReuseIdentifier:@"ReduceTicketPersonCell"];
    
    self.ticketTypeLabel.textColor = kSubitleColor;
}




+(CGFloat)getCellHeightWith:(NSInteger)personNum{
    
   
     return  (personNum + 2)*50;
    
}

- (IBAction)reduceBtnClicked:(id)sender {
    
    if (self.currentNum > 0) {
        
        self.currentNum -= 1;
        self.numLabel.text = [NSString stringWithFormat:@"%ld",self.currentNum];
        
        if (_delegate && [_delegate respondsToSelector:@selector(addMemBerType:WithNum:)]) {
            [_delegate addMemBerType:self.ticketType WithNum:self.currentNum];
        }
    }
}

- (IBAction)plusBtnClicked:(id)sender {
    
    self.currentNum += 1;
    self.numLabel.text = [NSString stringWithFormat:@"%ld",self.currentNum];
    if (_delegate && [_delegate respondsToSelector:@selector(addMemBerType:WithNum:)]) {
        
        [_delegate addMemBerType:self.ticketType WithNum:self.currentNum];
    }
}


- (void)setDataSource:(NSMutableArray *)dataSource {
    
    _dataSource = [NSMutableArray arrayWithArray:dataSource];
    self.numLabel.text = [NSString stringWithFormat:@"%ld",dataSource.count];
    [self.tableView reloadData];
}
- (void)configTicketType:(TicketTypeModel *)typeModel {
    
    self.needdetail = typeModel.needdetail;
    self.ticketTypeLabel.text =typeModel.title;
    
    self.ticketType = typeModel.type.integerValue;
    self.priceLabel.text = typeModel.smartPirce;
    if (typeModel.needdetail) {
        
        self.reduceBtn.hidden = YES;
        self.plusBtn.hidden = YES;
        self.tiecktNumView.layer.borderWidth = 0;
        //self.numLabel.layer.borderWidth = 1.f;
        self.numLabel.backgroundColor = kBGColor;
        self.numLabel.textColor = kContentColor;
    }else {
        self.reduceBtn.hidden = NO;
        self.plusBtn.hidden = NO;
        self.tiecktNumView.layer.borderWidth = 0;
        self.numLabel.layer.borderWidth = 0;
        self.numLabel.backgroundColor = kBGColor;
        self.numLabel.textColor = kContentColor;
    }
    [self.tableView reloadData];
}

- (void)setneeddetail:(BOOL)needdetail {
    _needdetail = needdetail;
    if (needdetail) {
        self.tiecktNumView.hidden = YES;
    }else {
        self.tiecktNumView.hidden = NO;
    }
}

#pragma mark --

- (void)deletTheMember:(TicketMemberInfoModel *)model {
    
    if (_delegate && [_delegate respondsToSelector:@selector(deleteMemberWith:WithType:)]) {
        [_delegate deleteMemberWith:model WithType:self.ticketType];
    }
    }
   



# pragma mark  -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.needdetail) {
        return 2;
    }else{
        return 0;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return self.dataSource.count;
        
    }else  if(section == 1){
        return 1;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        
        addTicketPersonsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"addTicketPersonsCell"];
        
        return cell;
    }else{
        
        ReduceTicketPersonCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ReduceTicketPersonCell"];
    
            TicketMemberInfoModel * model = self.dataSource[indexPath.row];
        
            cell.delegate = self;
            
            [cell configDataWith:model];
        
        
        return cell;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if ([cell isKindOfClass:[addTicketPersonsCell class]]) {
        
        if (_delegate && [_delegate respondsToSelector:@selector(addMemBerType:)]) {
            
            [_delegate addMemBerType:self.ticketType];
        }
        NSLog(@"添加新人员");
    }else{
        
    }
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
